const mysql = require("mysql2");
const http = require("http");

const conn = mysql.createConnection({
    host: "localhost", //域名
    user: "root", // 用户名
    password: "root", //密码(此处是你自己安装mysql时设置的)
    database: "ieduchina", //数据库（你自己创建的数据库）
    insecureAuth: true // 添加此选项
})

conn.connect(function (err) {
    if (!err) {
        console.log("连接数据库成功");
    }
});


var sql = "select * from news where 1";
var into = "INSERT INTO actively VALUES(6,'习近平：我是一贯支持民营企业的','2025-3-3 11:54:16');";

conn.query(into, function (err, res) {
    if (!err) {
        console.log(res);
    } else {
        console.log(err);
    }
});

const server = http.createServer(function (req, res) {
    res.statusCode = 200;
    res.setHeader("Content-Type", "application/json");
    conn.query(sql, function (err, result) {
        if (!err) {
            res.end(JSON.stringify(result));
        } else {
            res.end(`{"code":1,"msg":"数据库错误"}`);
        }
    })
})

server.listen(8888, function () {
    console.log("服务器", "localhost:8888", "启动成功");
});